﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using DTOService;
using System.Data;

namespace DAOService
{
    public class DAOLoaiBangDia : IDAOLoaiBangDia
    {
        Provider p = new Provider();

        public LoaiBangDia[] SelectLoaiBangDia()
        {
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_DanhSachLoaiBangDia");
                LoaiBangDia[] rs = new LoaiBangDia[dt.Rows.Count];
                LoaiBangDia temp;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    temp = new LoaiBangDia();
                    temp.MaLoaiBangDia = int.Parse(dt.Rows[i][0].ToString());
                    temp.TenLoaiBangDia = dt.Rows[i][1].ToString();
                    rs[i] = temp;
                }
                return rs;
            }
            catch
            {
                return null;
            }
        }

        public int UpdateLoaiBangDia(LoaiBangDia parr)
        {
            DataTable dt = p.ExecuteStoreProcedure("sp_CapNhatLoaiBangDia", new string[] { "maloaibangdia", "tenloaibangdia" }, new object[] { parr.MaLoaiBangDia, parr.TenLoaiBangDia });
            return int.Parse(dt.Rows[0][0].ToString());
        }

        public int DeleteLoaiBangDia(LoaiBangDia parr)
        {
            DataTable dt = p.ExecuteStoreProcedure("sp_XoaLoaiBangDia", new string[] { "maloaibangdia" }, new object[] { parr.MaLoaiBangDia });
            return int.Parse(dt.Rows[0][0].ToString());
        }

        public int InsertLoaiBangDia(LoaiBangDia parr)
        {
            DataTable dt = p.ExecuteStoreProcedure("sp_ThemLoaiBangDia", new string[] { "tenloaibangdia" }, new object[] { parr.TenLoaiBangDia });
            return int.Parse(dt.Rows[0][0].ToString());
        }

        public LoaiBangDia GetLoaiBangDia(int ma)
        {
            LoaiBangDia temp = new LoaiBangDia();
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_GetLoaiBangDia", new string[] { "@ma" }, new object[] { ma });
                if (dt.Rows.Count != 0)
                {
                    temp.MaLoaiBangDia = int.Parse(dt.Rows[0][0].ToString());
                    temp.TenLoaiBangDia = dt.Rows[0][1].ToString();
                }
            }
            catch (Exception ex)
            { throw new Exception(ex.Message); }
            return temp;
        }
    }
}
